package com.hmy.contract.manager.order;

import cn.hutool.json.JSONUtil;
import com.hmy.ccp.common.result.FeignResult;
import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.order.api.bo.request.SupplyOrderListDetailQueryApiRequest;
import com.hmy.order.api.bo.request.SupplyOrderSignContractRequest;
import com.hmy.order.api.bo.request.UpdateSupplyOrderContractRequest;
import com.hmy.order.api.bo.response.SupplyOrderPrimaryApiResponse;
import com.hmy.order.api.bo.response.SupplyOrderSignContractResponse;
import com.hmy.order.api.bo.response.UpdateSupplyOrderContractResponse;
import com.hmy.order.api.client.SupplyOrderServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author seven.qi 齐胜南
 * @version 1.0
 * @description
 * @date 2023/12/28 19:19
 */

@Component
@Slf4j
public class SupplyOrderFeignClientManager {

    @Resource
    private SupplyOrderServiceClient supplyOrderServiceClient;

    /**
     * 获取增补订单详细信息
     *
     * @param request 入参
     * @return {@link SupplyOrderPrimaryApiResponse}
     */
    public List<SupplyOrderPrimaryApiResponse> getSupplyOrderDetailByIds(SupplyOrderListDetailQueryApiRequest request) {
        log.info("SupplyOrderFeignClientManager.getSupplyOrderDetailByIds params:{}", JSONUtil.toJsonPrettyStr(request));
        FeignResult<List<SupplyOrderPrimaryApiResponse>> response = null;
        response = supplyOrderServiceClient.getSupplyOrderDetailByIds(request);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000032);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("SupplyOrderFeignClientManager.getSupplyOrderDetailByIds response:{}", JSONUtil.toJsonPrettyStr(response));
        return response.getData();
    }

    /**
     * 获取增补订单详细信息
     *
     * @param request 入参
     * @return {@link SupplyOrderPrimaryApiResponse}
     */
    public SupplyOrderSignContractResponse supplyOrderSignContract(SupplyOrderSignContractRequest request) {
        log.info("SupplyOrderFeignClientManager.supplyOrderSignContract params:{}", JSONUtil.toJsonPrettyStr(request));
        FeignResult<SupplyOrderSignContractResponse> response = null;
        response = supplyOrderServiceClient.supplyOrderSignContract(request);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000032);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("SupplyOrderFeignClientManager.supplyOrderSignContract response:{}", JSONUtil.toJsonPrettyStr(response));
        return response.getData();
    }

    /**
     * 获取增补订单详细信息
     *
     * @param request 入参
     * @return {@link SupplyOrderPrimaryApiResponse}
     */
    public UpdateSupplyOrderContractResponse updateSupplyOrderContract(UpdateSupplyOrderContractRequest request) {
        log.info("SupplyOrderFeignClientManager.updateSupplyOrderContract params:{}", JSONUtil.toJsonPrettyStr(request));
        FeignResult<UpdateSupplyOrderContractResponse> response = null;
        response = supplyOrderServiceClient.updateSupplyOrderContract(request);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000032);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("SupplyOrderFeignClientManager.updateSupplyOrderContract response:{}", JSONUtil.toJsonPrettyStr(response));
        return response.getData();
    }

}
